/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.shide.businessuser.mvvm.adpter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.shide.businessuser.GoodsModifyActivity;
import com.shide.businessuser.R;
import com.shide.businessuser.UI.PopWin_UpShelf_goods;
import com.shide.businessuser.UI.PopWin_downShelf_goods;
import com.shide.businessuser.UI.PopWin_laregePic;
import com.shide.businessuser.Utils.Utils;
import com.shide.businessuser.databinding.ItemGoodsListDatabindingBinding;
import com.shide.businessuser.http.GlobleURL;
import com.shide.businessuser.http.adapter.ClientManageAdapter_dbing;
import com.shide.businessuser.http.adapter.GoodlistAdapter;
import com.shide.businessuser.http.entity.Product;
import com.shide.businessuser.mvvm.model.Product_mvvm;
import com.shide.businessuser.mvvm.view.PopWin_UpShelf_goods_mvvm;
import com.shide.businessuser.mvvm.view.PopWin_downShelf_goods_mvvm;

import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author gehj
 * @date 2021/1/20 22:08
 */
//参考文献
//https://blog.csdn.net/cui130/article/details/79916558
public class GoodlistAdapter_mvvm extends BaseAdapter {
    private List<Product_mvvm> listProducts = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public GoodlistAdapter_mvvm(List<Product_mvvm> listProducts, Context context) {
        this.listProducts = listProducts;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }




    @Override
    public int getCount() {
        return listProducts.size();
    }

    @Override
    public Object getItem(int position) {
        return listProducts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemGoodsListDatabindingBinding binding = null;
        //IMPORTANT: databinding代替viewholer，可以看出 -
        //  recycleview 的com.shide.businessuser.http.adapter.ClientManageAdapter_dbing
        //  的模板写法是为了干啥
        if (convertView == null){
            binding = DataBindingUtil.inflate(layoutInflater,
                    R.layout.item_goods_list_databinding,
                    parent, false);
        }else {
            binding = DataBindingUtil.getBinding(convertView);
        }

        Product_mvvm product = listProducts.get(position);

        binding.setProduct(product);
        binding.executePendingBindings();

        String orderImgUrl = GlobleURL.getOrderImgUrl(product.getProductImg());
        /*Glide.with(context).load(orderImgUrl)
                .override(300,242)
                .centerCrop()
                .placeholder(R.drawable.ic_img_unload)
                .error(R.drawable.ic_img_unload)
                .into(binding.imgHead);*/
        //binding.imgHead.se

        /**
         * 以下取出bitmap，为了点击放大图片
         * */
        final Bitmap[] bitmap = {null};
         Glide.with(context).asBitmap().load(orderImgUrl).into(new SimpleTarget<Bitmap>() {
             @Override
             public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                 bitmap[0] =  resource ;
             }
         });


        ItemGoodsListDatabindingBinding finalBinding = binding;
        binding.imgHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bitmap[0] != null){
                    //CameraUtils.largePicture(context,bitmap[0], finalViewHolder1.imgHead);
                    PopWin_laregePic laregePic = new PopWin_laregePic(context,bitmap[0]);
                    laregePic.showPopupWindow(finalBinding.imgHead);
                }
            }
        });



        if (product.getShelf() == 1) {
            binding.tvShelf.setText("正常");
            binding.tvCreatTime.setText("上架时间："+product.getCreateTime());
        }else { //2 已下架
            binding.tvShelf.setText("已下架");
            binding.tvCreatTime.setText("下架时间："+product.getCreateTime());
        }


         ItemGoodsListDatabindingBinding finalBinding1 = binding;
         binding.btnItemMangeGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (product.getShelf() == 1){//正常 -->下架（1 --> 2）
                    //弹出下架pop
                    PopWin_downShelf_goods_mvvm pdg = new PopWin_downShelf_goods_mvvm(context);
                    pdg.setProduct(product);
                    pdg.showPopupWindow(finalBinding1.btnItemMangeGoods);


                }else {//下架 -->上架（2-->1）
                    // 弹出上架pop
                    PopWin_UpShelf_goods_mvvm popWin_upShelf_goods = new PopWin_UpShelf_goods_mvvm(context);
                    popWin_upShelf_goods.setProduct(product);
                    popWin_upShelf_goods.showPopupWindow(finalBinding1.btnItemMangeGoods);
                }
            }
        });


      /*  binding.btnItemUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsModifyActivity.class);
                intent.putExtra("id",product.getId());
                intent.putExtra("typeId",product.getExchangeTypeId());
                intent.putExtra("typeName",product.getTypeName());
                intent.putExtra("unit",product.getProductUnit());
                intent.putExtra("shelf",product.getShelf());
                intent.putExtra("creattime",product.getCreateTime());
                intent.putExtra("productImg",product.getProductImg());
                String TwoDicimalPrice = Utils.parseDoule2TwoDicimal(product.getExchangeAmount().toString());
                intent.putExtra("price",TwoDicimalPrice);
                intent.putExtra("description",product.getDescription());
                context.startActivity(intent);
            }
        });*/



        return binding.getRoot();
    }


}
